package com.yang.chen.oa.controller;

import com.alibaba.fastjson.JSONObject;
import com.yang.chen.oa.domain.TbSign;
import com.yang.chen.oa.service.SignService;
import com.yang.chen.oa.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class SignController {

    @Autowired
    private SignService signService;


    @RequestMapping("/sign/selectSign")
    public String selectSign() {
        return "sign/sign";
    }

    @RequestMapping("/sign/signJson")
    public @ResponseBody
    PageBean<TbSign> showAllDept(Integer pageNum, Integer pageSize, String startDate, String endDate, TbSign tbSign) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate_;
        Date endDate_;

        if(null == startDate || "".equals(startDate)) {
            startDate_ = new Date(1991, 10, 16);
        } else {
            startDate_ = simpleDateFormat.parse(startDate);
        }

        if (null == endDate || "".equals(endDate)) {
            endDate_ = new Date();
        } else {
            endDate_ = simpleDateFormat.parse(endDate);
        }

        PageBean<TbSign> pageBean = signService.selectPage(pageNum, pageSize, startDate_, endDate_, tbSign);
        return pageBean;

    }

    @RequestMapping("/sign/addSign")
    public void addDept(TbSign sign, HttpServletResponse response) {
        sign.setFlag(1);
        sign.setCreatetime(new Date());
        if (signService.addTbSign(sign)) {
            try {
                response.getWriter().print("success");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                response.getWriter().print("error");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/sign/decideSign")
    @ResponseBody
    public JSONObject checkExesit(TbSign sign, HttpServletResponse response, HttpServletRequest request) {
        request.getSession().getAttribute("user_session");
        JSONObject object = new JSONObject();
        sign.setCreatetime(new Date());
        if (signService.selectTbSign(sign)) {
            object.put("code", 1);
        } else {
            object.put("code", 0);
        }
        return object;
    }


}
